package com.github.kaelthasbmg.factory.generate.impl;

import com.github.kaelthasbmg.bean.Column;
import com.github.kaelthasbmg.bean.DataBaseDefine;
import com.github.kaelthasbmg.bean.PrimaryKey;
import com.github.kaelthasbmg.bean.Table;
import com.github.kaelthasbmg.factory.generate.Generator;

import java.sql.Connection;
import java.util.List;

/**
 * 针对PostgreSQL数据库的结构生成
 *
 * @author Carsymor
 */
public class PostgreSqlGenerator extends Generator {

    public PostgreSqlGenerator(Connection connection, DataBaseDefine dataBaseDefine) {
        super(connection, dataBaseDefine);
    }

    @Override
    protected String getTableDDL(Table tableDefine) {
        StringBuilder stringBuilder = new StringBuilder("create table ");
        stringBuilder.append(tableDefine.getTableName()).append("(");

        List<Column> columnList = tableDefine.getColumns();
        for (Column column : columnList) {
            stringBuilder.append(this.getColumnDefineDDL(column));
            stringBuilder.append(",");
        }

        PrimaryKey primaryKey = tableDefine.getPrimaryKey();
        if (primaryKey != null) {
            stringBuilder.append(this.createPrimaryKeyDefineDDL(primaryKey)).append(",");
        }

        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        stringBuilder.append(")");

        return stringBuilder.toString();
    }

    /**
     * 生成主键定义的DDL语句
     * @param primaryKey 主键定义
     * @return DDL语句
     */
    private String createPrimaryKeyDefineDDL(PrimaryKey primaryKey) {
        StringBuilder stringBuilder = new StringBuilder("primary key(");
        List<String> columnNames = primaryKey.getColumns();
        for (String columnName : columnNames) {
            stringBuilder.append(columnName).append(",");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        stringBuilder.append(")");

        return stringBuilder.toString();
    }

    /**
     * 生成字段定义的DDL语句
     * @param column 字段定义
     * @return DDL语句
     */
    private String getColumnDefineDDL(Column column) {
        StringBuilder stringBuilder = new StringBuilder(column.getColumnName());
        if (column.isTextType()) {
            stringBuilder.append(" text");
        } else if (column.isDateTimeType()) {
            stringBuilder.append(" timestamp");
        } else {
            stringBuilder.append(" ").append(column.getColumnType());

            if (column.notIntType()) {
                if (column.hasMaxLength()) {
                    stringBuilder.append("(").append(column.getMaxLength());
                }

                if (column.hasPrecision()) {
                    stringBuilder.append(",").append(column.getPrecision());
                }

                if (column.hasMaxLength()) {
                    stringBuilder.append(")");
                }
            }
        }

        if (!column.isNullAble()) {
            stringBuilder.append(" ").append("not null");
        }

        if (column.hasDefault()) {
            stringBuilder.append(" default ").append(column.getDefaultDefine());
        }

        return stringBuilder.toString();
    }
}
